import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

# -- coding: utf-8 --
import sys
import io
from dotenv import load_dotenv

# 设置标准输出的编码为UTF-8
if sys.stdout.encoding != "UTF-8":
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")

# 加载环境变量
load_dotenv()


def test_langchain_setup():
    """测试LangChain基础设置"""
    print("🧪 测试LangChain与DeepSeek连接...")

    # 初始化DeepSeek模型（通过OpenAI兼容接口）
    model = ChatOpenAI(
        model="deepseek-chat",
        openai_api_key=os.getenv("DEEPSEEK_API_KEY"),
        openai_api_base="https://api.deepseek.com/v1",
        temperature=0.7,
    )

    # 测试可用
    try:
        from langchain_core.messages import HumanMessage

        messages = [HumanMessage(content="你好，请用一句话介绍你自己")]
        response = model.invoke(messages)

        print("✅ LangChain连接成功！")
        print("🤖 模型回复:", response.content)
        return model
    except Exception as e:
        print(f"❌ 连接失败: {e}")
        return None


if __name__ == "__main__":
    model = test_langchain_setup()
    if model:
        print("\n🎉 基础环境测试通过！可以继续下一步学习。")
